package com.example.pluginapp.controller;

import com.example.pluginapp.PluginAppApplication;
import org.pf4j.JarPluginManager;
import org.pf4j.PluginManager;
import org.pf4j.ZipPluginManager;
import org.pf4j.demo.api.IPf4jGreeting;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.File;
import java.nio.file.Paths;
import java.util.List;

@RestController
public class HelloController {
    private static final String dir = "E:\\workspaceidea\\pf4jdemo";


    @RequestMapping("/jar")
    public String jar() {
//        PluginManager pluginManager = new JarPluginManager();
//        // 加载指定路径插件
//        pluginManager.loadPlugin(Paths.get(dir + "\\plugin1-1.0-SNAPSHOT.jar"));
//        // 启动指定插件(也可以加载所有插件)
//        pluginManager.startPlugin("welcome-plugin");
//        // 执行插件
//        List<IPf4jGreeting> greetings = pluginManager.getExtensions(IPf4jGreeting.class);
//        for (IPf4jGreeting greeting : greetings) {
//            greeting.sayHello();
//        }
//        // 停止并卸载指定插件
//        pluginManager.stopPlugin("welcome-plugin");
//        pluginManager.unloadPlugin("welcome-plugin");
//
//        System.out.println("jar");

        //        String property = System.getProperty("user.dir");
//        System.out.println("property: " + property);
        String path = PluginAppApplication.class.getProtectionDomain().getCodeSource().getLocation().getPath();
        System.out.println("path: " + path);
        // 加载指定路径插件
//        pluginManager.loadPlugin(Paths.get("E:\\workspaceidea\\pf4jdemo\\plugin1-1.0-SNAPSHOT.jar"));
        String jarPath = "";
        String osName = System.getProperties().getProperty("os.name");
        if (osName.equals("linux")) {

        } else {
            jarPath = path.substring(1) + "plugin1-1.0-SNAPSHOT.jar";
        }
        System.out.println("jarPath: " + jarPath);

        PluginManager pluginManager = new JarPluginManager();
        pluginManager.loadPlugin(Paths.get(jarPath));
        // 启动指定插件(也可以加载所有插件)
        pluginManager.startPlugin("welcome-plugin");
        // 执行插件
        List<IPf4jGreeting> greetings = pluginManager.getExtensions(IPf4jGreeting.class);
        for (IPf4jGreeting greeting : greetings) {
            greeting.sayHello();
        }
        // 停止并卸载指定插件
        pluginManager.stopPlugin("welcome-plugin");
        pluginManager.unloadPlugin("welcome-plugin");
        return "jar";
    }

    @RequestMapping("/zip")
    public String zip() {

        String path = PluginAppApplication.class.getProtectionDomain().getCodeSource().getLocation().getPath();
        System.out.println("path: " + path);
        // 加载指定路径插件
//        pluginManager.loadPlugin(Paths.get("E:\\workspaceidea\\pf4jdemo\\zip\\plugin1-1.0-SNAPSHOT.jar.zip"));
        String zipPath = "";
        String osName = System.getProperties().getProperty("os.name");
        if (osName.equals("linux")) {

        } else {
            zipPath = path.substring(1) + "zip" + File.separator + "plugin1-1.0-SNAPSHOT.jar.zip";
        }
        System.out.println("jarPath: " + zipPath);

        PluginManager pluginManager = new ZipPluginManager();
        // 加载指定路径插件
        pluginManager.loadPlugin(Paths.get(zipPath));
        // 启动指定插件(也可以加载所有插件)
        pluginManager.startPlugin("welcome-plugin");
        // 执行插件
        List<IPf4jGreeting> greetings = pluginManager.getExtensions(IPf4jGreeting.class);
        for (IPf4jGreeting greeting : greetings) {
            greeting.sayHello();
        }
        // 停止并卸载指定插件
        pluginManager.stopPlugin("welcome-plugin");
        pluginManager.unloadPlugin("welcome-plugin");

        System.out.println("zip");
        return "zip";
    }
}
